//                       .::::.
//                     .::::::::.
//                    :::::::::::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                ..::::::::::::.
//              ``::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ```` ':.          ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
package com.xw.service;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Maps;
import com.xw.dao.UserMapper;
import com.xw.dao.WithdrawMapper;
import com.xw.model.User;
import com.xw.model.Withdraw;
import com.xw.util.AppUtil;
import com.xw.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class WithdrawServiceImpl implements WithdrawService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private WithdrawMapper withdrawMapper;

    @Override
    public Map withdrawByUserId(String userId, String realName, Double money, Integer withdrawType, String account) {
        //验证是否拉黑
        if (!StringUtils.equals("-1",userId) && StringUtils.isNotBlank(userId)) {
            User user = userMapper.selectByPrimaryKey(userId);
            if (user.getUserStatus() == 2) {
                return JsonData.returnMap(811, "您的账号已被锁定");
            }
        }
        Map map = Maps.newHashMap();

        //每天提现不能超过3次
        String today = DateUtil.today();
        Integer count = withdrawMapper.findCountByTime(userId,today);
        if (count >=3){
            return JsonData.returnMap(500,"每天提现不能超过3次");
        }
        User user = userMapper.selectByPrimaryKey(userId);
        if (user.getBalance()<money || money <1){
            map.put("code",810);
            map.put("msg","账户金额不足,且提现金额最低1元");
            return map;
        }
        Withdraw withdraw = new Withdraw();
        withdraw.setId(AppUtil.uuid());
        withdraw.setUserId(userId);
        withdraw.setRealName(realName);
        withdraw.setMoney(money);
        withdraw.setWithdrawType(withdrawType);
        withdraw.setAccount(account);
        withdraw.setCreateTime(new Date());
        Integer flag = withdrawMapper.insertSelective(withdraw);
        return JsonData.returnMap(flag);
    }
}
